package com.functest.orm

import com.functest.framework.BaseFunctionalTest
import com.functest.orm.TestData.Student
import com.functest.orm.TestDataForCalculatedFields.Numbers
import org.testng.annotations.Test

/**
 * Created with IntelliJ IDEA.
 * User: MalyutinAA
 * Date: 10.07.13
 * Time: 13:07
 * To change this template use File | Settings | File Templates.
 */
class BaseProviderByFieldValueTest extends BaseFunctionalTest {

    @Test
    void byFieldValePositive(){
        asTest {
            List<Student> students

            asStep('test byFieldValue with one result'){
                students = Student.provider.byFieldValue('id', 2)
                assert students.firstName == ['Ivan']
                assert students.lastName == ['Ivanov']
                assert students.status == [TestData.StudentStatus.Graduate]
            }

            asStep('test byFieldValue with several results'){
                students = Student.provider.byFieldValue('status', TestData.StudentStatus.Student)
                assert students.id ==[-1,0,3]
            }

            asStep('test byFieldValue with value as Closure'){
                String name = 'Iv'
                Closure fullName = {name + 'an'}
                students = Student.provider.byFieldValue('firstName', fullName)
                assert students.id == [2]
                assert students.lastName == ['Ivanov']
                assert students.status == [TestData.StudentStatus.Graduate]
            }

//            List<Numbers> numbers
//            asStep('test byFieldValue with calculated field'){
//                numbers = Numbers.provider.byFieldValue('lastText', 'word_2')
//                assert numbers.num == [2]
//                assert numbers.firstText ==['word']
//            }
        }
    }

    @Test
    void byFieldValueNegative(){
        asTest{
            List<Student> students

            asStep('test byFieldValue without results'){
                students = Student.provider.byFieldValue('id', 10)
                assert students == []
            }

            asStep('test byFieldValue with nonexistent field'){
                try{
                    students = Student.provider.byFieldValue('age', 20)
                }
                catch(Throwable e){
                    assert e instanceof NoSuchFieldException
                    assert e.message == 'No such field defined: age'
                }
            }

            asStep('test byFieldValue with null field'){
                try{
                    students = Student.provider.byFieldValue( null, 20)
                }
                catch(Throwable e){
                    assert e instanceof NullPointerException
                    assert e.getMessage() == 'Cannot invoke method indexOf() on null object'
                }
            }

            asStep('test byFieldValue with null value'){
                try{
                    students = Student.provider.byFieldValue('id', null)
                }
                catch(Throwable e){
                    assert e instanceof IllegalArgumentException
                    assert e.getMessage() == 'Value cannot be null for EQ in buildSql [[]]'
                }
            }

            List<Numbers> numbers
            asStep('test byFieldValues with calculated fields without result'){
                numbers = Numbers.provider.byFieldValue('lastText', 'word_3')
                assert numbers == []
            }
        }
    }

}
